package com.jokey.vue.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jokey.vue.dto.EmpPageReq;
import com.jokey.vue.mapper.EmployeeMapper;
import com.jokey.vue.model.Employee;
import com.jokey.vue.model.RespPageBean;
import com.jokey.vue.service.EmployeeService;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

/**
 * @author JokeyFeng
 * @date: 2021/11/27
 * @project: vuehr
 * @package: com.jokey.vue.service.impl
 * @comment:
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

	DecimalFormat decimalFormat = new DecimalFormat("##.00");
	
	@Override
	public boolean save(Employee employee) {
		LocalDate beginContract = employee.getBeginContract();
		LocalDate endContract = employee.getEndContract();
		long month = ChronoUnit.MONTHS.between(beginContract, endContract);
		employee.setContractTerm(Double.parseDouble(decimalFormat.format(month / 12)));
		return super.save(employee);
	}
	
	@Override
	public RespPageBean<Employee> queryPage(EmpPageReq pageReq) {
		PageInfo<Employee> pageInfo = PageHelper.startPage(pageReq.getPage(), pageReq.getSize())
				.doSelectPageInfo(() -> baseMapper.getEmployeeList(pageReq));
		RespPageBean<Employee> pageBean = RespPageBean.build();
		pageBean.setTotal(pageInfo.getTotal());
		pageBean.setList(pageInfo.getList());
		return pageBean;
	}
	
	@Override
	public Integer maxWorkId() {
		return baseMapper.maxWorkId();
	}
}
